<?php
$str = "abababcd";
$pattern = '/^((ab)|(cd))+$/';
$res = preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
var_dump($res, $matches);

//$rep = preg_replace_callback($pattern, function($matches3) {
//    var_dump($matches3);
//    return 'xxx';
//} , $str, -1, $count);
//
//var_dump($rep, $count);